package com.gxuwz.medical.domain.money;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.exception.DbException;
import com.mysql.fabric.xmlrpc.base.Data;

public class Money {
	
	private String id;
	private String name;
	private String standard;
	private String lasttime;
	private String effectiveage;
	private String pay;
	private String aggregate;
	private String invoicenumber;
	private String operator;
	
	private String cmscode;
	/*
	private String relative;
	private String membercode;
	private String familyholdername;
	private String idcard;
	private String sex;
	private String familycode;*/

	public Money(){
		
	}

	public String getId() {
		return id;
	}



	public void setId(String id) {
		this.id = id;
	}



	public String getName() {
		return name;
	}



	public void setName(String name) {
		this.name = name;
	}



	public String getStandard() {
		return standard;
	}



	public void setStandard(String standard) {
		this.standard = standard;
	}



	public String getLasttime() {
		return lasttime;
	}



	public void setLasttime(String lasttime) {
		this.lasttime = lasttime;
	}



	public String getEffectiveage() {
		return effectiveage;
	}



	public void setEffectiveage(String effectiveage) {
		this.effectiveage = effectiveage;
	}



	public String getPay() {
		return pay;
	}



	public void setPay(String pay) {
		this.pay = pay;
	}



	public String getAggregate() {
		return aggregate;
	}



	public void setAggregate(String aggregate) {
		this.aggregate = aggregate;
	}



	public String getInvoicenumber() {
		return invoicenumber;
	}



	public void setInvoicenumber(String invoicenumber) {
		this.invoicenumber = invoicenumber;
	}



	public String getOperator() {
		return operator;
	}



	public void setOperator(String operator) {
		this.operator = operator;
	}



	public String getCmscode() {
		return cmscode;
	}



	public void setCmscode(String cmscode) {
		this.cmscode = cmscode;
	}



	public Money(String id, String name, String standard, String lasttime,
			String effectiveage, String pay, String aggregate,
			String invoicenumber, String operator, String cmscode) {
		super();
		this.id = id;
		this.name = name;
		this.standard = standard;
		this.lasttime = lasttime;
		this.effectiveage = effectiveage;
		this.pay = pay;
		this.aggregate = aggregate;
		this.invoicenumber = invoicenumber;
		this.operator = operator;
		this.cmscode = cmscode;
	}

	@Override
	public String toString() {
		return "Money [id=" + id + ", name=" + name + ", standard=" + standard
				+ ", lasttime=" + lasttime + ", effectiveage=" + effectiveage
				+ ", pay=" + pay + ", aggregate=" + aggregate
				+ ", invoicenumber=" + invoicenumber + ", operator=" + operator
				+ ", cmscode=" + cmscode + "]";
	}

	private void loadDB(String id) throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn
					.prepareStatement("select * "
							+ "from t_join_money "
							+ "where id=?");
			pstmt.setString(1, this.id);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				this.id = rs.getString("id");
				this.name = rs.getString("name");
				this.standard = rs.getString("standard");
				this.lasttime = rs.getString("lasttime");
				this.effectiveage = rs.getString("effectiveage");
				this.pay = rs.getString("pay");
				this.aggregate = rs.getString("aggregate");
				this.invoicenumber = rs.getString("invoicenumber");
				this.operator = rs.getString("operator");
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
	}
	
	public Money(String id) throws DbException {
		try {
			this.id = id;
			loadDB(id);
		} catch (SQLException e) {
			throw new DbException("加载缴费信息=" + id + "失败", e);
		}
	}
	
	public void add() throws Exception {
		Connection conn = null;
		try {
			// 保存导数据库
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			// 调用插入数据库私有方法
			this.saveToDB(conn);
			conn.commit();
		} catch (Exception e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}

	private void saveToDB(Connection connection) throws SQLException {
		PreparedStatement pstmt = null;
		try {

			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append("insert into t_join_money(name,standard,lasttime,effectiveage,"
					+ "pay,aggregate,invoicenumber,operator,cmscode)");
			sqlBuff.append("values(?,?,?,?,?,?,?,?,?)");
			String sql = sqlBuff.toString();
			pstmt = connection.prepareStatement(sql);
			int i = 1;
			pstmt.setString(i++, this.name);
			pstmt.setString(i++, this.standard);
			pstmt.setString(i++, this.lasttime);
			pstmt.setString(i++, this.effectiveage);
			pstmt.setString(i++, this.pay);
			pstmt.setString(i++, this.aggregate);
			pstmt.setString(i++, this.invoicenumber);
			pstmt.setString(i++, this.operator);
			pstmt.setString(i++, this.cmscode);
			// 以executeUpdate()执行SQL
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			// 只关闭PrepareStatement资源
			if (pstmt != null) {
				pstmt.close();
			}

		}

	}
	
	public void edit(String id,String name, String standard, String lasttime,
			String effectiveage, String pay, String aggregate,
			String invoicenumber, String operator) throws SQLException {
		Connection conn = null;
		try {
			this.id=id;
			this.name = name;
			this.standard = standard;
			this.lasttime = lasttime;
			this.effectiveage = effectiveage;
			this.pay = pay;
			this.aggregate = aggregate;
			this.invoicenumber = invoicenumber;
			this.operator = operator;
			System.out.println("咋传到实体里面没值了大哥=="+id+name+standard);
			conn =DbUtil.getConn();
			conn.setAutoCommit(false);
			this.updateToDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	private void updateToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			// 定义构造update SQL语句字符串变量sql
			String sql = "update t_join_money set name=?, standard=?,lasttime=?,effectiveage=?,"
					+ "pay=?,aggregate=?,invoicenumber=?,operator=? where id=?";
			// 创建执行带动态参数的SQL的PreparedStatement pstmt
			pstmt = conn.prepareStatement(sql);
			// 设置动态参数对应的值
			int i = 1;
			pstmt.setString(i++, this.name);
			pstmt.setString(i++, this.standard);
			pstmt.setString(i++, this.lasttime);
			pstmt.setString(i++, this.effectiveage);
			pstmt.setString(i++, this.pay);
			pstmt.setString(i++, this.aggregate);
			pstmt.setString(i++, this.invoicenumber);
			pstmt.setString(i++, this.operator);
			pstmt.setString(i++, this.id);
			System.out.println("你在哪你在哪的编辑sql"+sql);
			int count = pstmt.executeUpdate();

		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}

	}
}
